package Search;

public class SelectSort {

    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};

        System.out.println("排序前的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }

//        selectionSort(arr);
        insertionSort(arr);

        System.out.println("\n排序后的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }

    private static void insertionSort(int[] arr) {
        int n = arr.length;
        for (int i = 1; i < n; i++) {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && key < arr[j]) {
                arr[j + 1] = arr[j];
                j -= 1;
            }

            arr[j+1] = key;
        }
    }


    private static void selectionSort(int[] arr) {
        int n = arr.length;

        int t = 0;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex])
                    minIndex = j;
            }

            t = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = t;

        }
    }


}
